package cn.doit.scala.day03

import scala.collection.immutable
import scala.io.Source

/**
 * @DATE 2021/12/27/9:51
 * @Author MDK
 * @Version 2021.2.2
 * */
object C02_MapFunction {
  def main(args: Array[String]): Unit = {
    val array: Array[Int] = Array[Int](1, 2, 3, 4, 5, 6, 7, 8)
    val ls = List[String]("zss","lss","ny","ly","fjj")
    val mp = Map[String, String]("sl"->"fsy","wd"->"zsf","em"->"zzr","gb"->"qf")

    val res = array.map(e => e * 10)
//简写: val res = array.map(_*10)
    val res2 = ls.map(e => e.toUpperCase)
//简写: val res = ls.map(_.toUpperCase)
    val res3 = mp.map(tp=>{
      val newV = tp._2.toUpperCase
      (tp._1, newV)
    })
    println(res3)

    val res4: immutable.Iterable[String] = mp.map(tp=>tp._2)
    println(res4)

    //变成四个元素的对偶元组
    val res5 = mp.map(tp=>{
      val k = tp._1
      val v = tp._2
      val k2 = k.toUpperCase
      val v2 = v.toUpperCase
      (k,v,k2,v2)
    })
    println(res5.toList)

    val br = Source.fromFile("data/stu.txt")
    val lines = br.getLines()
    val tuple: Iterator[(String, String, Double)] = lines.map(line =>{
      val arr = line.split("\\s+")
      (arr(0),arr(1),arr(2).toDouble*10)
    })
    tuple.foreach( tp=> println(tp._2+"\t"+tp._3))
  }
}
